package com.kdd.campus.sh.controller;

import com.kdd.campus.common.entity.CommonResult;
import com.kdd.campus.sh.dto.ESProductInfo;
import com.kdd.campus.sh.po.ShKeywords;
import com.kdd.campus.sh.po.ShRecord;
import com.kdd.campus.sh.service.ShKeywordsService;
import com.kdd.campus.sh.service.ShProductService;
import com.kdd.campus.sh.service.ShRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Auther: fcp
 * @Date: 2020/3/25 21:06
 * @Description:
 */
@Api(value = "搜索")
@RestController
@RequestMapping("/search")
public class ShSearchController {
    @Autowired
    private ShKeywordsService shKeywordsService;

    @Autowired
    private ShRecordService shRecordService;

    @Autowired
    private ShProductService shProductService;

    @Value("${product.pageSize}")
    private int pageSize;

    @Value("${search.historyPageSize}")
    private int historyPageSize;

    /***
     * 查询ShKeywords全部数据
     * @return
     */
    @ApiOperation(value = "查询所有热搜")
    @GetMapping("hotList")
    public CommonResult<List<ShKeywords>> listHot(){
        //调用ShKeywordsService实现查询所有ShKeywords
        List<ShKeywords> list = shKeywordsService.findHot();
        return CommonResult.success(list);
    }

    /***
     * 查询历史记录
     * @return
     */
    @ApiOperation(value = "查询搜索历史(查10条)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userId", value = "用户id", dataType = "String"),
  })
    @GetMapping("history")
    public CommonResult<List<ShRecord>> getHistory(String userId){
        List<ShRecord> list = shRecordService.findHistory(userId,historyPageSize);
        return CommonResult.success(list);
    }

    @ApiOperation(value = "删除指定历史记录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "historyId", value = "历史记录id", dataType = "Long"),
    })
    @DeleteMapping("history/{historyId}")
    public CommonResult<List<ShRecord>> deleteHistory(@PathVariable(required = true) Long historyId){
        shRecordService.deleteHistory(historyId);
        return CommonResult.success();
    }

    @ApiOperation(value = "清空历史记录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userId", value = "用户id", dataType = "Long"),
    })
    @DeleteMapping("history/empty")
    public CommonResult<List<ShRecord>> emptyHistory(@RequestParam(required = true) Long userId){
        shRecordService.emptyHistory(userId);
        return CommonResult.success();
    }


    @ApiOperation(value = "关键词搜索(搜索商品)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userId", value = "用户id", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "keyword", value = "关键词", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页码(从0开始)", dataType = "Integer")
    })
    @GetMapping("keyword")
    public CommonResult<List<ESProductInfo> > searchByKeyword(Long userId , @RequestParam(required = true) String keyword, @RequestParam(defaultValue = "0") Integer pageNum){
        List<ESProductInfo> list = shProductService.searchByKeyword(userId,keyword,pageNum,pageSize);
        return CommonResult.success(list);
    }



}
